package com.ooqiu.gaming.ui.bind;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.base.BaseActivity;
import com.ooqiu.gaming.common.entities.GamingUser;
import com.ooqiu.gaming.components.retrofit.progress.ProgressSubscriber;
import com.ooqiu.gaming.components.storage.UserStorage;
import com.ooqiu.gaming.utils.RxCountDown;
import com.ooqiu.gaming.view.ToastUtil;
import com.ooqiu.gaming.view.Topbar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

@Route(path = BindPhoneActivity.PATH)
public class BindPhoneActivity extends BaseActivity {
    public static final String PATH = "/gn/bind_mobile";
    @BindView(R.id.topbar)
    Topbar mTopbar;
    @BindView(R.id.edit_mobile)
    EditText mEditMobile;
    @BindView(R.id.edit_phone_code)
    EditText mEditPhoneCode;
    @BindView(R.id.tv_countdown)
    TextView mTvCountdown;
    @BindView(R.id.btn_login)
    Button mBtnLogin;


    @Autowired(name = "login_type")
    int login_type;

    @Autowired(name = "token")
    String token;


    GamingApi mGamingApi;

    UserStorage mUserStorage;

    @Override
    public int initContentView() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public void initInjector() {
        ButterKnife.bind(this);
        mGamingApi = getApplicationComponent().getGamingApi();
        mUserStorage = getApplicationComponent().getUserStorage();
    }

    @Override
    public void initUiAndListener() {
        mTopbar.setOnMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    protected Toolbar toolbarIdRes() {
        return mTopbar;
    }

    @Override
    protected View statusLayoutId() {
        return null;
    }


    @Override
    public Drawable getStateBarDrawable() {
        return new ColorDrawable(Color.WHITE);
    }


    @OnClick(R.id.btn_login)
    public void bindPhone() {
        if (TextUtils.isEmpty(mEditMobile.getText())) {
            ToastUtil.showWarning("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(mEditPhoneCode.getText())) {
            ToastUtil.showWarning("请输入验证码");
            return;
        }


        mGamingApi.bindPhone(login_type, token, mEditMobile.getText().toString(), mEditPhoneCode.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<GamingUser>(mContext) {
                    @Override
                    public void onNext(GamingUser user) {
                        mUserStorage.saveUser(user);
                        setResult(Activity.RESULT_OK);
                        finish();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    @OnClick(R.id.tv_countdown)
    public void fetchCode() {
        if (TextUtils.isEmpty(mEditMobile.getText())) {
            ToastUtil.showWarning("请输入手机号");
            return;
        }
        mGamingApi.fetchCode(mEditMobile.getText().toString(), 3, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<String>(mContext) {
                    @Override
                    public void onNext(String object) {
                        countdown();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }

    private void countdown() {
        RxCountDown.countdown(60)
                .subscribe(new DefaultObserver<Integer>() {
                    @Override
                    protected void onStart() {
                        mTvCountdown.setEnabled(false);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        mTvCountdown.setText(String.valueOf(integer));
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        mTvCountdown.setText("获取");
                        mTvCountdown.setEnabled(true);
                    }

                });
    }

}
